package com.nf.health.app.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.models.BloodPressureHistoryBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepan on 2016/3/21.
 * The only genius that is worth anything is the genius for hard work
 */
public class BloodPressureHistoryDataAdapter extends BaseAdapter {
    private Activity context;
    private BloodPressureHistoryBean bean;
    //设备数据集合
    private List<BloodPressureHistoryBean.DataBean.DeviceDataBean> deviceDatas;

    public BloodPressureHistoryDataAdapter(Activity context, BloodPressureHistoryBean bean) {
        this.context = context;
        this.bean = bean;
        deviceDatas = bean.data.deviceData;
    }

    private ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();
    /**
     * 设置适配器数据来源
     *
     * @param extra
     */
    public void setExtraData(BloodPressureHistoryBean extra) {
        this.bean = extra;
        deviceDatas = bean.data.deviceData;
    }

    @Override
    public int getCount() {
        return deviceDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        BloodPressureHistoryBean.DataBean.DeviceDataBean deviceDataBean = deviceDatas.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_blood_pressure_history_data_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvDate.setText(deviceDataBean.posttime.substring(5, 16));
        holder.tvLowPressure.setText(deviceDataBean.low == 0.0?  "":deviceDataBean.low+"");
        holder.tvHighPressure.setText(deviceDataBean.high == 0.0?  "":deviceDataBean.high + "");
        holder.tvHeartRate.setText(deviceDataBean.heart_rate == 0.0?  "":deviceDataBean.heart_rate + "");
                    //删除对应position数据
                    holder.delete.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (listener != null)
                    listener.onClick(position, "");
                closeAllOpenedSwipeView();
                unClosedSwipeView.clear();
            }
        });
        holder.swipeView.setOnSwipeStatusChangeListener(new SwipeView.OnSwipeStatusChangeListener() {

            @Override
            public void onSwiping(SwipeView swipingSwipeView) {
                if (!unClosedSwipeView.contains(swipingSwipeView)) {
                    closeAllOpenedSwipeView();
                }

                if (!unClosedSwipeView.contains(swipingSwipeView)) {
                    unClosedSwipeView.add(swipingSwipeView);
                }

            }

            @Override
            public void onOpen(SwipeView openedSwipeView) {
                for (int i = 0; i < unClosedSwipeView.size(); i++) {
                    if (unClosedSwipeView.get(i) != openedSwipeView) {
                        unClosedSwipeView.get(i).close();
                    }
                }
                if (!unClosedSwipeView.contains(openedSwipeView)) {
                    unClosedSwipeView.add(openedSwipeView);
                }
            }

            @Override
            public void onClose(SwipeView closedSwipeView) {
                unClosedSwipeView.remove(closedSwipeView);
            }
        });
        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.tv_high_pressure)
        TextView tvHighPressure;
        @Bind(R.id.tv_low_pressure)
        TextView tvLowPressure;
        @Bind(R.id.tv_heart_rate)
        TextView tvHeartRate;
        @Bind(R.id.delete)
        TextView delete;
        @Bind(R.id.swipeView)
        SwipeView swipeView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private OnMyClickListener listener;
    public void setOnMyClickListener(OnMyClickListener listener){
        this.listener = listener;
    }
    private void closeAllOpenedSwipeView() {
        for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }
    }
}
